//彩宝
var APP=is_app();
var app_url = location.origin;
var RATE= Number($.cookie('GemRate')) || 1;
 var TEAM=0;
 var PlaceOrder=false;//代下单
(function () {
    var gems = {
        init: function () {
        	args = GetUrlParms();				
			if(args["team"]!=undefined){						
				 TEAM = args["team"];//产品ID					
			} 
			if(args["cid"]!=undefined){						
				 Customer_id = args["cid"];	
				 PlaceOrder=true;//代下单
			} 
            this.bindEvents();
            this.loadGems();
        },
        bindEvents: function () { 
            var self = this;
            //筛选
            $("#term_app_btn").on("click",function(){
            	if(PlaceOrder==true){
            		location.href="/Customer/GemFilter?team=3&cid="+Customer_id;
            	}else{
            		location.href="/Customer/GemFilter?team="+TEAM;
            	}
            });
           //app
           $("#gems").on("click","#app_link_item",function(){
           		var link_url=$(this).attr("data-src");
           		if(APP){
           			var appJson={};
           			appJson.title="彩宝详情";
           			appJson.url=app_url+link_url;	
           			YiYi_APP.showNewPage(JSON.stringify(appJson));
           		}else{
           			location.href=link_url;
           		}       
           });
            //点击菜单
			$("#show_menu_btn").on("click",function(){
				$("#assistant_menu_box").toggle();
			});
			//关闭菜单
			$("#assistant_menu_box").on("click",function(){
				$("#assistant_menu_box").hide();
			});
			//新增
			$("#add_btn").on("click",function(){
				var link_url="/Team/GemManagement?type=4";
				if(APP){
					var appJson={};
           			appJson.title="彩宝新增";
           			appJson.url=app_url+link_url;	
           			YiYi_APP.showNewPage(JSON.stringify(appJson));
				}else{
					location.href=link_url;
				}
			});
        },

        //加载数据
        loadGems: function (first) {
            var self = this;
            myApp.showIndicator();//显示加载中
            var params = PARAMS;//APP手机过来的参数

         	if(params==null){
         		params={};
         	}
         	if(TEAM==2){
         		params.IsMyStock=true;//现货
         	}else{
         		params.OnShelves = "1"; 
         	}
         	if(TEAM==0){
         		params.CustomerID = CustomerID;//客户ID
         	}
            params.useDict = true;
            params.GoodsType=4;
            var params = JSON.stringify(params);
            var url ="/Team/GetProductData";
            $.ajax({
                url: url,
                data: params,
                type: "POST",
                contentType: "application/json; charset=utf8",
                success: function (result) {
                    $("#gems").html('');
                    tempSupplier = 0;
                    var res=eval('(' + result + ')')
                    if (res.msgcode == true) {
                        window.GOODS_ITEMS = res.rows;
                        var dtRenderStart = new Date().getTime();
                        isSearching = false;
                        if (res.msginfo.rows.length == 0) {
                            $("#gems").html('<div class="no_data_tip">没有符合条件的结果</div>');
                        } else {
                            var gems = [];
                            for (var i = 0; i < res.msginfo.rows.length; i++) {
                                var row = res.msginfo.rows[i];
                                gems.push(
                                        '<li data="', row.Supplier, '" itemtype="" itemid="', row.ID, '" class="',i%2==0?"":"mark",'">',
                                        	'<a href="javascript:void(0)" data-src=',"/",TEAM>0?"Team":"Customer","/GemDetails/?ID=",row.ID,"&team=",TEAM,PlaceOrder?"&cid="+Customer_id:"",' id="app_link_item" data-type="'+GetCategory(row.Category)+'">',
                                        	'<div id="image_app_box" class="image_app_box">',
                                        		'<img class="product_img lazy lazy-fadein" data-src="', imgSrc(row.Images), '" src="#" alt="彩宝"/>',
                                        	'</div>',
                                        	'<div class="product_data">',
		                                		'<h4>',
                                    				'<span class="CurrentRMB">￥<b title="价格">'+formatRMBPrice(row.Price)+'</b></span>',
		                                			'<span class="CurrentUSD">$<b title="美金价格">'+formatUSDPrice(row.Price/RATE)+'</b></span>',
		                                		'</h4>',
		                                		'<div class="data_box">',
		                               				'<div class="series">',row.Series,'</div>',
		                                			'<div>',
		                                				'<b title="重量">',row.Weight,'</b>',
		                                				'<b title="颜色">' ,GetText("gems",row.Category,row.Color,"Color"),'</b>',
		                               					'<b title="产地">',row.Place,'</b>',
		                               					processText(row.Category,row.Process,row.Oiliness),
		                               				'</div>',
//		                                			'<div class="supplier_box">',
//		                                				'<b title="供应商">',ConvertSupplier(row),'</b>',
//		                                			'</div>',
		                                		'</div>',
		                                	'</div>',
		                                '</a></li>'
                                     );
                            }

                            $("#gems").html(gems.join(''));
                            currentShow();  //币种设置
                            //懒加载
                             var n = 0,
						     imgNum = $("#gems li").length,
						     img = $('#gems li .product_img');
						    myApp.initImagesLazyLoad(img)
						    $$('img.lazy').trigger('lazy');
//						    //图片加载出来时居中显示 放在 framework.js
                        }

                        console.info(new Date().getTime() - dtRenderStart + "ms");
                    } else {
                    	myApp.alert(res.msginfo);                      
                    }
                    myApp.hideIndicator();//隐藏加载中
                }
            });
        },

    };
    gems.init();
})();
//没权限返回的价格
function returnPrice(data){
	//if(data.Price || data.Price==0){
		return '<span class="CurrentRMB">￥<b title="价格">'+formatRMBPrice(data.Price)+'</b></span>';		
	//}else{
	//	return '<span style="font-size:14px;">￥<b title="价格">'+('权限不足')+'</b></span>';	
	//}
}

//优先显示简称
function ConvertSupplier(data) {
//	if(data.Supplier != undefined) {
//		if(data.ShortName != "") {
//			return data.ShortName;
//		} else {
			return data.Supplier;
		//}
//	} else
//		return "-";
}

//处理转换
function processText(type,val,oil) {
	if(type==1||type==2){
	    return ['<b title="处理">', GetText("gems", type, val, "Process"), '</b>'].join('');
	}else if(type==3){
	    return ['<b title="油性">', GetText("gems", type, oil, "Oiliness"), '</b>'].join('');
	}
}

//返回图片链接
function imgSrc(images) {
    if (images) {
        images = images.split('|');
        var small = images[0];
        var big = small.replace('400x400', '1920x1920');
        return small;
    }
    return '../images/no_img.jpg';//无图时返回默认图片
}
